using System;
using System.Collections.Generic;
using System.Windows.Input;
using BusinessIntelligenceToolkit.ViewModels;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;

namespace BusinessIntelligenceToolkit.Demo
{
    public class WeatherForecastViewModel : NotificationObject
    {
        public const string MoveToFirstParam = "First";
        public const string MoveToLastParam = "Last";
        public const string MoveToNextParam = "Next";
        public const string MoveToPreviousParam = "Previous";

        private const int MoveToDefaultIndex = 10;

        private readonly Random _random = new Random();
        private int _generatedItemsCount = 20;

        private string _moveToIndex = MoveToDefaultIndex.ToString();
        private NavigableListCollectionView _history;
        private string _generateItemsCountInput;

        private DelegateCommand _generate;
        private ICommand _moveTo;
        private DelegateCommand<string> _move;

        public WeatherForecastViewModel()
        {
            GenerateData();
        }

        public DelegateCommand Generate
        {
            get { return _generate ?? (_generate = new DelegateCommand(GenerateExecute, CanGenerateExecute)); }
        }

        public ICommand MoveTo
        {
            get { return _moveTo ?? (_moveTo = new DelegateCommand(MoveToExecute, CanMoveToExecute)); }
        }

        public DelegateCommand<string> Move
        {
            get { return _move ?? (_move = new DelegateCommand<string>(MoveExecute, CanMoveExecute)); }
        }

        public NavigableListCollectionView History
        {
            get
            {
                return _history;
            }

            set
            {
                _history = value;
                RaisePropertyChanged(() => History);
            }
        }

        public string GenerateItemsCountInput
        {
            get
            {
                return _generateItemsCountInput;
            }

            set
            {
                _generateItemsCountInput = value;
                Generate.RaiseCanExecuteChanged();
                RaisePropertyChanged(() => GenerateItemsCountInput);
            }
        }

        public string MoveToIndex
        {
            get
            {
                return _moveToIndex;
            }

            set
            {
                _moveToIndex = value;
                RaisePropertyChanged(() => MoveToIndex);
            }
        }

        private void GenerateData()
        {
            var history = new List<HistoryItem>();
            DateTime fromDate = DateTime.Now;
            for (int i = 0; i < _generatedItemsCount; i++)
            {
                history.Add(new HistoryItem()
                {
                    Number = (i + 1).ToString(),
                    DateTime = fromDate,
                    Weather = (Weather)_random.Next(0, 3)
                });
                fromDate = fromDate.AddHours(-1);
            }

            History = new NavigableListCollectionView(history);
            GenerateItemsCountInput = _generatedItemsCount.ToString();
        }

        private bool CanGenerateExecute()
        {
            bool success = Int32.TryParse(GenerateItemsCountInput, out _generatedItemsCount);
            return success;
        }

        private void GenerateExecute()
        {
            GenerateData();
        }

        private bool CanMoveToExecute()
        {
            return true;
        }

        private void MoveToExecute()
        {
            int moveToIndex;
            bool success = int.TryParse(MoveToIndex, out moveToIndex);
            if (success)
            {
                if (moveToIndex - 1 >= 0 && moveToIndex - 1 < History.Count)
                {
                    History.MoveCurrentToWithBringing(History.GetItemAt(moveToIndex - 1));
                }
            }
            else
            {
                MoveToIndex = MoveToDefaultIndex.ToString();
            }
        }

        private bool CanMoveExecute(string param)
        {
            return true;
        }

        private void MoveExecute(string param)
        {
            switch (param)
            {
                case MoveToFirstParam:
                    History.MoveCurrentToFirstWithBringing();
                    break;
                case MoveToLastParam:
                    History.MoveCurrentToLastWithBringing();
                    break;
                case MoveToNextParam:
                    History.MoveCurrentToNextWithBringing();
                    break;
                case MoveToPreviousParam:
                    History.MoveCurrentToPreviousWithBringing();
                    break;
            }
        }
    }
}